package com.cqeec.myblog.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cqeec.myblog.model.DataDictionary;
import com.cqeec.myblog.service.ArticleService;
import com.cqeec.myblog.service.ArticleServiceImpl;

@WebServlet("/data/dictionary/management.html")
public class DataDictionaryServlet extends HttpServlet {
	private static final long serialVersionUID = -5044754722571387627L;
	private ArticleService articleService = new ArticleServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String type = req.getParameter("type");
		String m = req.getParameter("m");
		String name = req.getParameter("name");
		String message = ""; // 消息
		int code = 0; // 代码：默认值为零，表示执行成功。
		StringBuffer data = new StringBuffer(); // 返回数据
		if ("ArticleType".equals(type)) {
			// 文章类型
			if ("list".equals(m)) {
				// 获取所有文章类型
				try {
					List<DataDictionary> list = articleService.getArticleType();
					// 转为JSON数据
					DataDictionary dd;
					for (int i = 0; i < list.size(); i++) {
						dd = list.get(i);
						data.append("<tr><td>" + (i + 1) + "</td><td>" + dd.getName()
								+ "</td><td><a id=\'"+dd.getId()+"\' class=\'btn btn-danger\'><i class=\'far fa-trash-alt\'></i></a></td></tr>");
					}
					code = 0;
					message = "获取文章类型数据成功！";
				} catch (Exception e) {
					e.printStackTrace();
					code = 1;
					message = "获取文章类型数据失败！";
				}
			} else if ("add".equals(m)) {
				DataDictionary dd = new DataDictionary();
				dd.setId(UUID.randomUUID().toString().toUpperCase());
				dd.setName(name);
				dd.setType("ArticleType");
				// 新增文章类型
				try {
					articleService.addArticleType(dd);
					// 把成功信息回馈给用户
					code = 0;
					message = "添加文章分类成功！";
					data.append(dd.getId());
				} catch (Exception e) {
					e.printStackTrace();
					// 把错误信息回馈给用户
					code = 1; // code为1时表示添加文章分类失败
					message = "添加文章分类失败！";
				}
			} else if ("del".equals(m)) {
				// 删除文章类型
				String id = req.getParameter("id");
				try {
					articleService.delArticleType(id);
					code = 0;
					message = "删除文章分类成功！";
				} catch (Exception e) {
					e.printStackTrace();
					code = 1;
					message = "删除文章分类失败！";
				}
			}
		} else if ("Gender".equals(type)) {
			// 性别
			if ("add".equals(m)) {
				// 新增文章类型
			} else if ("del".equals(m)) {
				// 删除文章类型
			}
		}
		// 返回信息到前端页面
		resp.setCharacterEncoding("UTF-8");
		PrintWriter out = resp.getWriter();
		// out.write(code + "," + message);
		// 按照JSON的格式封装字符串
		out.write("{\"code\":" + code + ",\"message\":\"" + message + "\",\"data\":\"" + data.toString() + "\"}");
	}

}
